package com.fesine.produce;

import java.util.concurrent.LinkedBlockingQueue;

/**
 * @description: 使用阻塞队列blockingqueue,使用put和take方法
 * @author: Fesine
 * @createTime:2017/7/29 0:10
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2017/7/29 0:10
 */
public class Storage3 {
    private final int MAX_SIZE = 100;

    //仓库载体，指定大小100
    private LinkedBlockingQueue<Object> list = new LinkedBlockingQueue<>(100);
    public void produce(int num){
        if (list.size() == MAX_SIZE) {
            System.out.println(Thread.currentThread().getName()+ "\t[库存量]" + list.size()
                    + "\t暂时不能执行生产任务");
        }
        for (int i = 1; i <= num; ++i) {
            try {
                list.put(new Object());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName() + "\t[要生产的商品数量]" + num + "\t生产结束【现仓储量为】:" + list.size());
    }

    public void consume(int num){
        if (list.size() == 0) {
            System.out.println(Thread.currentThread().getName()  + "\t[库存量]" + list.size()
                    + "\t暂时不能执行消费任务");
        }
        for (int i = 1; i <= num; ++i) {
            try {
                list.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName() + "\t[要消费的商品数量]" + num + "\t消费结束【现仓储量为】:" + list.size());
    }

    public int getMAX_SIZE() {
        return MAX_SIZE;
    }

    public LinkedBlockingQueue<Object> getList() {
        return list;
    }

    public void setList(LinkedBlockingQueue<Object> list) {
        this.list = list;
    }
}
